package com.hxzy.dao.impl;

import com.hxzy.bean.Message;
import com.hxzy.bean.User;
import com.hxzy.dao.MessageDao;
import com.hxzy.util.CommonUtil;
import com.hxzy.util.JdbcUtils;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author nick
 */
public class MessageDaoImpl implements MessageDao {
    @Override
    public int save(Message message) {
        String sql = "INSERT INTO message(`topic`,`content`,mes_date,author_id) values(?,?,?,?)";
        Object[] params = {message.getTopic(),message.getContent(),message.getMesDate(),message.getAuthor().getId()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int update(Message message) {
        return 0;
    }

    @Override
    public int remove(Integer integer) {
        return 0;
    }

    @Override
    public List<Message> queryAll() {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" m.`id` as id,")
                .append(" m.`topic` as topic,")
                .append(" m.`content` as content,")
                .append(" m.`mes_date` as mesDate,")
                .append(" m.`author_id` as authorId,")
                .append(" u.`nick_name` as nickName")
                .append(" FROM message as m INNER JOIN message_users u")
                .append(" ON m.author_id = u.id")
                .append(" ORDER BY m.`mes_date` DESC");
        //System.out.println(builder.toString());

        List<Map<String, Object>> list = JdbcUtils.executeQuery(builder.toString());
        return list.stream()
                .map(t -> getMessage(t)) //将每一个Map集合转换为Message对象
                .collect(Collectors.toList());//终止遍历，将转换后的stream收集为List<Message>的集合
    }

    @Override
    public Message findById(Integer integer) {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" m.`id` as id,")
                .append(" m.`topic` as topic,")
                .append(" m.`content` as content,")
                .append(" m.`mes_date` as mesDate,")
                .append(" m.`author_id` as authorId,")
                .append(" u.`nick_name` as nickName,")
                .append(" u.`pic` as pic")
                .append(" FROM message as m INNER JOIN message_users u")
                .append(" ON m.author_id = u.id")
                .append(" WHERE m.`id`=?");
        System.out.println(builder.toString());
        List<Map<String, Object>> list = JdbcUtils.executeQuery(builder.toString(),integer);
        Optional<Message> first = list.stream().map(t -> getMessage(t)).findFirst();
        return first.orElse(null);
    }

    private Message getMessage(Map<String, Object> t) {
        int id = ((Long) (t.get("id"))).intValue();
        String topic = (String) t.get("topic");
        String content = (String) t.get("content");
        Timestamp date = (Timestamp) t.get("mesDate");
        LocalDateTime localDateTime = CommonUtil.timeStamp2LocalDateTime(date);

        int authorId = ((Long) (t.get("authorId"))).intValue();//留言人
        String nickName = (String) t.get("nickName");
        String pic = (String) t.get("pic");
        User user = new User();
        user.setId(authorId);
        user.setNickName(nickName);
        user.setPic(pic);

        //填充message对象
        return new Message(id, topic, content, localDateTime, user);
    }

    @Override
    public List<Message> paging(int pageIndex, int pageSize) {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" m.`id` as id,")
                .append(" m.`topic` as topic,")
                .append(" m.`content` as content,")
                .append(" m.`mes_date` as mesDate,")
                .append(" m.`author_id` as authorId,")
                .append(" u.`nick_name` as nickName,")
                .append(" u.`pic` as pic")  //增加查询头像的列
                .append(" FROM message as m INNER JOIN message_users u")
                .append(" ON m.author_id = u.id")
                .append(" ORDER BY m.`mes_date` DESC")
                .append(" LIMIT ?,?");  //(当前页 - 1) * 每页显示的条数, 每页显示的条数
        //System.out.println(builder.toString());
        Object[] params = {
                (pageIndex - 1) * pageSize, //排除前多少条数据
                pageSize    //取几条数据
        };
        List<Map<String, Object>> list = JdbcUtils.executeQuery(builder.toString(),params);
        return list.stream()
                .map(t -> getMessage(t)) //将每一个Map集合转换为Message对象
                .collect(Collectors.toList());//终止遍历，将转换后的stream收集为List<Message>的集合

    }

    @Override
    public int totals() {
        String sql = "select COUNT(0) total FROM message"; //聚合函数，返回单行单列的结果
        Map<String, Object> map = JdbcUtils.executeQuery(sql).get(0);
        Long l = (Long) map.get("total");
        return l.intValue();
    }
}
